<?php

require_once ROOT_SERVICES_MODEL . DS . 'models_oms.php';
require_once ROOT_SERVICES_MODEL . DS . 'models_rule.php';
require_once ROOT_SERVICES_MODEL . DS . 'models_location.php';
require_once ROOT_SERVICES_MODEL . DS . 'models_setting.php';
require_once ROOT_SERVICES . DS . 'delivery_promises.php';
require_once ROOT_LIBRARY_KLOGGER . DS . 'klogger.php';
require_once ROOT_LIBRARY_LAZADA . DS . 'global_vars.php';

class service_interface {

    public static function get_time_shipping($db, $cache, $params) {
        try {
            if (isset($params['sku']) && isset($params['location'])) {

                $oms = new models_oms($cache);
                $rule_model = new models_rule($db, $cache);
                $location_model = new models_localtion($db, $cache);
                $date = isset($params['date']) ? $params['date'] : date('Y-m-d');

                if (!$location_model->check_exist($params['location'])) {
                    return array('error' => 1, 'message' => 'location does not exist', 'data' => '');
                }

                $delivery = new delivery_promises($cache, $oms, $location_model, $rule_model);
                $result = $delivery->get_time($params['sku'], $params['location'], $date);

                $gvars = global_vars::instance();
                if ($gvars->get('debug')) {
                    return array('error' => 0, 'message' => '', 'data' => $result, 'debug' => $gvars->get("rule_{$params['sku']}_{$params['location']}"));
                }
                return array('error' => 0, 'message' => '', 'data' => $result);
            } else {
                return array('error' => 1, 'message' => 'invalidate params', 'data' => '');
            }
        } catch (PDOException $pe) {
            if (strpos($pe->getMessage(), 'sku_dose_not_exist') !== false) {
                throw new Exception('sku does not exist');
            }
        }
    }

    public static function get_time_all_location($db, $cache, $params) {
		
        if (isset($params['sku'])) {
            
            
            $oms = new models_oms($cache);
            $setting = new models_setting($db, $cache);
            $rule_model = new models_rule($db, $cache);
            $location_model = new models_localtion($db, $cache);           
            $date = isset($params['date']) ? $params['date'] : date('Y-m-d');

            $delivery = new delivery_promises($cache, $oms, $location_model, $rule_model);
            $results = $delivery->get_time_all_location($params['sku'], $date);
            $respone_data = $results;
            
            // respone html.
            if (isset($params['format']) && $params['format'] == 'html') {
                $str_html = '<select id="select-prov" name="select-prov">';
                
                foreach ($results as $location) {

                    $int_day_max = ceil(intval(trim($location['result']['result_max'])) / 24);
                    $int_day_min = ceil(intval(trim($location['result']['result_min'])) / 24);

                    $str_display = '';
                    if($location['result']['result_type'] == delivery_promises::RULE_RESULT_TYPE_AMOUNT_OF_TIME){
                        $str_display = $setting->get('text_display');
                    }else{
                        $str_display = $location['result']['text_display'];
                    }
                    
                    $str_display = str_replace('%max', $int_day_max, $str_display);
                    $str_display = str_replace('%min', $int_day_min, $str_display);
                 
                    $str_html .= '<option value="' . $str_display . '">' . $location['location'] . '</option>';
                }
                $str_html .= '</select>';
                $respone_data = $str_html;
            }
            
            return array('error' => 0, 'message' => '', 'data' => $respone_data);
        } else {
            return array('error' => 1, 'message' => 'no parameter', 'data' => '');
        }
    }

}

?>
